package org.dromara.oce.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.oce.domain.OceAccount;
import org.dromara.oce.domain.bo.OceAccountBo;
import org.dromara.oce.domain.qo.OceAccountQo;
import org.dromara.oce.domain.vo.OceAccountVo;
import org.dromara.oce.mapper.OceAccountMapper;
import org.dromara.oce.service.OceAccountService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 巨量管家账号Service业务层处理
 *
 * @author dsbb
 * @date 2025-05-30
 */
@RequiredArgsConstructor
@Service
public class OceAccountServiceImpl extends ServiceImpl<OceAccountMapper, OceAccount> implements OceAccountService {

    private final OceAccountMapper baseMapper;

    /**
     * 查询巨量管家账号
     *
     * @param accountRecordId 主键
     * @return 巨量管家账号
     */
    @Override
    public OceAccountVo queryById(Long accountRecordId){
        return baseMapper.selectVoById(accountRecordId);
    }

    /**
     * 分页查询巨量管家账号列表
     *
     * @param qo        查询条件
     * @param pageQuery 分页参数
     * @return 巨量管家账号分页列表
     */
    @Override
    public TableDataInfo<OceAccountVo> queryPageList(OceAccountQo qo, PageQuery pageQuery) {
        LambdaQueryWrapper<OceAccount> lqw = buildQueryWrapper(qo);
        Page<OceAccountVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的巨量管家账号列表
     *
     * @param qo 查询条件
     * @return 巨量管家账号列表
     */
    @Override
    public List<OceAccountVo> queryList(OceAccountQo qo) {
        LambdaQueryWrapper<OceAccount> lqw = buildQueryWrapper(qo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OceAccount> buildQueryWrapper(OceAccountQo qo) {
        LambdaQueryWrapper<OceAccount> lqw = Wrappers.lambdaQuery();
//        lqw.eq(qo.getAdvertiserId() != null, OceAccount::getAdvertiserId, qo.getAdvertiserId());
//        lqw.like(StringUtils.isNotBlank(qo.getAdvertiserName()), OceAccount::getAdvertiserName, qo.getAdvertiserName());
//        lqw.eq(StringUtils.isNotBlank(qo.getAdvertiserRole()), OceAccount::getAdvertiserRole, qo.getAdvertiserRole());
//        lqw.eq(qo.getValidStatus() != null, OceAccount::getValidStatus, qo.getValidStatus());
//        lqw.like(StringUtils.isNotBlank(qo.getDisplayName()), OceAccount::getDisplayName, qo.getDisplayName());
//        lqw.eq(qo.getUserId() != null, OceAccount::getUserId, qo.getUserId());
//        lqw.eq(StringUtils.isNotBlank(qo.getEmail()), OceAccount::getEmail, qo.getEmail());
//        lqw.eq(StringUtils.isNotBlank(qo.getAppId()), OceAccount::getAppId, qo.getAppId());
//        lqw.eq(qo.getMaterialAuthStatus() != null, OceAccount::getMaterialAuthStatus, qo.getMaterialAuthStatus());
        return lqw;
    }

    /**
     * 新增巨量管家账号
     *
     * @param bo 巨量管家账号
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(OceAccountBo bo) {
        OceAccount add = MapstructUtils.convert(bo, OceAccount.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        return flag;
    }

    /**
     * 修改巨量管家账号
     *
     * @param bo 巨量管家账号
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(OceAccountBo bo) {
        OceAccount update = MapstructUtils.convert(bo, OceAccount.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OceAccount entity){
    }

    /**
     * 校验并批量删除巨量管家账号信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
